package com.lzw.mall.common.service.impl;

import com.lzw.mall.common.entity.Member;
import com.lzw.mall.common.exception.NoLoginException;
import com.lzw.mall.common.entity.Cart;
import com.lzw.mall.common.mapper.CartMapper;
import com.lzw.mall.common.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzw.mall.common.vo.CartVO;
import com.lzw.mall.common.vo.OrderSessionUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhenwei
 * @since 2023-04-04
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Override
    public void addToCart(Integer productId,Integer amount) {

        Member m = OrderSessionUtil.getCurrent();
        if (m == null) {//meidenglu
            throw new NoLoginException();
        }
        Cart cart = new Cart();
        Cart c=baseMapper.selectMemberAndProductId(m.getId(),productId);
        if (c!=null) {

            baseMapper.updateByAmount(c.getId(),c.getAmount()+amount);
        }else {
            cart.setProductId(productId);
            cart.setAmount(amount);
            cart.setMemberId(m.getId());
            baseMapper.insert(cart);
        }





    }

    @Override
    public List<CartVO> selectList() {

        Member m=OrderSessionUtil.getCurrent();
        return baseMapper.selectCartList(m.getId());
    }

    @Override
    public void doDel(Integer cartid) {
        baseMapper.doDel(cartid);
    }

    @Override
    public void updataAmount(Integer cartid, Integer amount) {
        baseMapper.updataAmount(cartid,amount);
    }

    @Override
    public List<CartVO> selectListByIds(String ids) {
        Member m=OrderSessionUtil.getCurrent();
        String[] arr=ids.split(",");
        return baseMapper.selectCartListByIds(m.getId(),arr);

    }
}
